const axios =require("axios");
const url = require("url"); 
async function getLocation(req) {
    const query = url.parse(req.url).query.split("&");
    //存储对象形式的经纬度坐标
    const lnglat = {}
    for(i in query){
       query[i] = query[i].split("=")
    }
    lnglat[query.flat()[0]] = query.flat()[1];
    lnglat[query.flat()[2]] = query.flat()[3];
    
    // console.log(result)

    //获取地理位置
    const {data: resLocation} = await axios.get(`https://restapi.amap.com/v3/geocode/regeo?key=f58766337111f56d9101db384fbea982&location=${lnglat.lng},${lnglat.lat}`);
    const location = resLocation.regeocode.addressComponent;
    return location
}

module.exports = getLocation;